package com.xwaf.platform.web.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.api.constants.BaseConstants;
import com.xwaf.platform.common.api.entity.ELUser;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.entity.DataBase;
import com.xwaf.platform.system.api.entity.ELProject;
import com.xwaf.platform.system.api.enums.OperateLogModelEnum;
import com.xwaf.platform.system.api.service.ELProjectService;
import com.xwaf.platform.system.api.service.auth.OperateLogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@RestController
@Api(tags = "项目信息", hidden = true)
@RequestMapping(value = "/x/project/")
public class ELProjectController extends BaseController {
	@Autowired
	private ELProjectService projectService;

	@Autowired
	private OperateLogService logService;

	@ApiOperation(value = "分页查询", response = ELProject.class)
	@PostMapping(value = "page", produces = "application/json;charset=UTF-8")
	public CommonResponse page(@RequestParam("currentPage") String currentPage,
			@RequestParam("pageSize") String pageSize, @RequestBody ELProject project) {
		if (!StringUtils.isNotBlank(currentPage) & !StringUtils.isNotBlank(pageSize)) {
			currentPage = "1";
			pageSize = "10";
		}
		Page<ELProject> page = new Page<ELProject>(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
		Page<ELProject> result = projectService.selPageList(page, project);
		return CommonResponse.createCommonResponse(result);
	}

	@ApiOperation(value = "查询所有项目", response = ELProject.class)
	@PostMapping(value = "list", produces = "application/json;charset=UTF-8")
	public CommonResponse list(@RequestBody ELProject project) {
		Wrapper<ELProject> wrapper = new EntityWrapper<ELProject>();
		wrapper.where("id={0}", 10).like("name", "6");
		List<ELProject> list = projectService.selectList(wrapper);
		return CommonResponse.createCommonResponse(list);
	}

	@ApiOperation(value = "查询单个项目基本信息", response = ELProject.class)
	@GetMapping(value = "info/{projectId}", produces = "application/json;charset=UTF-8")
	public CommonResponse getInfoById(@PathVariable("projectId") Integer projectId) {
		ELProject project = projectService.selectById(projectId);
		return CommonResponse.createCommonResponse(project);
	}

	@ApiOperation("新增项目基本信息")
	@PostMapping(value = "add", produces = "application/json;charset=UTF-8")
	public CommonResponse addProject(@RequestBody ELProject project) {
		project.setId(DataBase.getDataBaseId(DataBase.ET));
		project.setCreateTime(new Date());
		project.setUpdateTime(new Date());
		boolean isAdded = projectService.insert(project);
		if (isAdded) {
			ELUser loginUser = getLoginUser();
			logService.insertLog(OperateLogModelEnum.EL_PROJECT.getModule(), "新增项目", getRemoteAddr(),
					"新增项目【" + project.getName() + "】", loginUser != null ? loginUser.getId() : null);
			return CommonResponse.createCommonResponse(project);
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "新增项目失败!");
		}
	}

	@ApiOperation("修改项目基本信息")
	@PutMapping(value = "update", produces = "application/json;charset=UTF-8")
	public CommonResponse updateProject(@RequestBody ELProject project) {
		project.setUpdateTime(new Date());
		boolean isAdded = projectService.updateById(project);
		if (isAdded) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "修改项目失败!");
		}
	}

	@ApiOperation("删除项目")
	@DeleteMapping(value = "delete/{projectId}", produces = "application/json;charset=UTF-8")
	public CommonResponse delById(@PathVariable("projectId") String projectId) {
		boolean isDel = projectService.deleteById(projectId);
		if (isDel) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "删除项目失败!");
		}
	}
}